home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1992 June: ROMin Holiday / ADC Developer CD (1992-06) (''ROMin Holiday'')_iso / Developer Connection - 06-1992.iso / Tools & Apps / Networking & Communications / TCPTool / TCPSend.c < prev    next >
Encoding:
Text File  |  1990-09-14  |  3.2 KB  |  131 lines  |  [TEXT/MPS ]

  1. //
  2. // MPW Tool by im.
  3. // Usage : TCPSend
  4. // 
  5.  
  6. #include    <QuickDraw.h>
  7. #include    <Devices.h>
  8. #include    <stdio.h>
  9. #include    <string.h>
  10.  
  11. #include    <MacTCPCommonTypes.h>
  12. #include    <AddressXlation.h>
  13. #include    <GetMyIPAddr.h>
  14. #include    <TCPPB.h>
  15. #include    <UDPPB.h>
  16.  
  17. #define        _STORAGE_    true
  18. #include    <TCP.h>
  19.  
  20. main (int argc, char *argv[]) {
  21.     
  22.     auto    OSErr            osErr            = noErr;
  23.     auto    short            index;
  24.     auto    char            *option;
  25.     auto    char            *parameter;
  26.     auto    char            *address;
  27.     auto    char            *text;
  28.     auto    TCPNotifyProc    asrProc            = nil;
  29.     auto    TCPiopb            pb;
  30.     auto    StreamPtr        stream;
  31.     auto    char            streamBuf[4096];
  32.     auto    long            streamBufLen    = sizeof(streamBuf);
  33.     auto    Ptr                streamBufPtr;
  34.     auto    ip_addr            localIP            = cAnyIP;
  35.     auto    ip_port            localPort        = cAnyPort;
  36.     auto    ip_addr            remoteIP        = cAnyIP;
  37.     auto    ip_port            remotePort        = cReceivePort;
  38.     auto    WDS(1)            wds;
  39.     
  40.     InitGraf((Ptr) &qd.thePort);
  41.  
  42.     index = 1;
  43.     while (index < argc) {
  44.     
  45.         option = argv[index++];
  46.         parameter = argv[index++];
  47.         
  48.         if ('-' != option[0] || (! strchr("atn", option[1])) || index > argc) {
  49.             printf("TCPSend -a address -t text [-n]");
  50.             exit(1);
  51.         } else {
  52.             switch (option[1]) {
  53.                 case 'a' :
  54.                     address = parameter;
  55.                     break;
  56.                 case 't' :
  57.                     text = parameter;
  58.                     break;
  59.                 case 'n' :
  60.                     asrProc = ASR;
  61.                     index--;
  62.                     break;
  63.             }
  64.         }            
  65.     }
  66.  
  67.     osErr = _TCPInit();
  68.     if (noErr == osErr) {
  69.         osErr = _TCPCreate(&pb, &stream, streamBuf, streamBufLen, (TCPNotifyProc) asrProc, (Ptr) nil,  (TCPIOCompletionProc) nil, false);
  70.         if (noErr == osErr) {
  71.             osErr = TCPDotAddress(address, &remoteIP);
  72.             if (noErr == osErr) {
  73.                 osErr = _TCPActiveOpen(&pb, stream, remoteIP, remotePort, &localIP, &localPort, (Ptr) nil, (TCPIOCompletionProc) nil, false);
  74.                 if (noErr == osErr) {
  75.                 
  76.                     /* send data to the remote host */
  77.                     wds.block[0].ptr    = text;
  78.                     wds.block[0].length    = strlen(text) + 1;
  79.                     wds.zero            = nil;
  80.                     osErr = _TCPSend(&pb, stream, (wdsEntry *) &wds, (Ptr) nil, (TCPIOCompletionProc) nil, false);
  81.                     
  82.                     osErr = _TCPClose(&pb, stream, nil, (TCPIOCompletionProc) nil, false);
  83.                 }
  84.             }
  85.         }
  86.         osErr = _TCPRelease(&pb, stream, &streamBufPtr, &streamBufLen, (TCPIOCompletionProc) nil, false);
  87.     }
  88.     exit(osErr);
  89. }
  90.  
  91.  
  92. pascal void    StrToAddrResultProc(aHostInfo, userdata)    /* utility routine for StrToAddr */
  93.     struct hostInfo    *aHostInfo;
  94.     Ptr                userdata;
  95. {
  96.     /* why bother when we can simply watch the aHostInfo.rtnCode? */
  97. }
  98.  
  99. OSErr TCPDotAddress(char *dotAddress, ip_addr *ipAddress) {
  100.     auto    OSErr                osErr;
  101.     auto    struct hostInfo        aHostInfo;            /* a data structure for the DNR function */
  102.  
  103.     osErr = OpenResolver((char *) 0);
  104.     if (osErr) {
  105.         return osErr;
  106.     }
  107.     
  108.     /* ask the DNR function to get the IP address */
  109.     StrToAddr(dotAddress, &aHostInfo, (ResultProcPtr) StrToAddrResultProc, (Ptr) 0);
  110.     
  111.     /* wait for the address information or some error other than cacheFault to occur */
  112.     while (cacheFault == aHostInfo.rtnCode)
  113.         ;
  114.     
  115.     osErr = CloseResolver();
  116.     if (osErr) {
  117.         return osErr;
  118.     }
  119.  
  120.     /* if it was an error there isn't much more we can do here but let the caller know */
  121.     if (noErr != aHostInfo.rtnCode) {
  122.         osErr = aHostInfo.rtnCode;
  123.         return osErr;
  124.     }
  125.     
  126.     /* use the first IP address for this host */
  127.     *ipAddress = aHostInfo.addr[0];
  128.                 
  129.     return osErr;
  130. }
  131.